<?php declare(strict_types=1);

/**
 * @package Giscus (Light Portal)
 * @link https://custom.simplemachines.org/index.php?mod=4244
 * @author Bugo <bugo@dragomano.ru>
 * @copyright 2023-2025 Bugo
 * @license https://spdx.org/licenses/GPL-3.0-or-later.html GPL-3.0-or-later
 *
 * @category plugin
 * @version 29.10.25
 */

namespace LightPortal\Plugins\Giscus;

use Bugo\Compat\Lang;
use Bugo\Compat\Utils;
use LightPortal\Enums\PluginType;
use LightPortal\Plugins\Event;
use LightPortal\Plugins\Plugin;
use LightPortal\Plugins\PluginAttribute;
use LightPortal\Plugins\SettingsFactory;
use LightPortal\Utils\Setting;

if (! defined('LP_NAME'))
	die('No direct access...');

/**
 * Generated by PluginMaker
 */
#[PluginAttribute(type: PluginType::COMMENT)]
class Giscus extends Plugin
{
	private string $url = 'https://giscus.app';

	private array $themes = [
		'light'                  => 'GitHub Light',
		'light_high_contrast'    => 'GitHub Light High Contrast',
		'light_protanopia'       => 'GitHub Light Protanopia & Deuteranopia',
		'light_tritanopia'       => 'GitHub Light Tritanopia',
		'dark'                   => 'GitHub Dark',
		'dark_high_contrast'     => 'GitHub Dark High Contrast',
		'dark_protanopia'        => 'GitHub Dark Protanopia & Deuteranopia',
		'dark_tritanopia'        => 'GitHub Dark Tritanopia',
		'dark_dimmed'            => 'GitHub Dark Dimmed',
		'preferred_color_scheme' => 'System (auto)',
		'transparent_dark'       => 'Transparent Dark',
		'noborder_light'         => 'NoBorder Light',
		'noborder_dark'          => 'NoBorder Dark',
		'noborder_gray'          => 'NoBorder Gray',
		'cobalt'                 => 'RStudio Cobalt',
		'purple_dark'            => 'Purple Dark',
		'gruvbox'                => 'Gruvbox',
		'gruvbox_dark'           => 'Gruvbox Dark',
		'gruvbox_light'          => 'Gruvbox Light',
		'catppuccin_latte'       => 'Catppuccin Latte',
		'catppuccin_frappe'      => 'Catppuccin Frappé',
		'catppuccin_macchiato'   => 'Catppuccin Macchiato',
		'catppuccin_mocha'       => 'Catppuccin Mocha',
		'fro'                    => 'Fro',
	];

	public function init(): void
	{
		Lang::$txt['lp_comment_block_set'][$this->name] = 'Giscus';
	}

	public function addSettings(Event $e): void
	{
		$this->addDefaultValues(['theme' => 'preferred_color_scheme']);

		$e->args->settings[$this->name] = SettingsFactory::make()
			->text('repo', [
				'subtext'  => sprintf($this->txt['repo_subtext'], $this->url),
				'required' => true,
			])
			->text('repo_id', [
				'subtext'  => sprintf($this->txt['repo_id_subtext'], $this->url),
				'required' => true,
			])
			->text('category', [
				'subtext'  => sprintf($this->txt['category_subtext'], $this->url),
				'required' => true,
			])
			->text('category_id', [
				'subtext'  => sprintf($this->txt['category_id_subtext'], $this->url),
				'required' => true,
			])
			->select('theme', $this->themes)
			->toArray();
	}

	public function comments(): void
	{
		if (Setting::getCommentBlock() !== $this->name)
			return;

		if (empty($this->context['repo']) || empty($this->context['repo_id']))
			return;

		if (empty($this->context['category']))
			return;

		if (empty($this->context['category_id']))
			return;

		Utils::$context['lp_giscus_comment_block'] = /** @lang text */ '
			<div class="giscus windowbg"></div>
			<script src="https://giscus.app/client.js"
				data-repo="' . $this->context['repo'] . '"
				data-repo-id="' . $this->context['repo_id'] . '"
				data-category="' . $this->context['category'] . '"
				data-category-id="' . $this->context['category_id'] . '"
				data-mapping="title"
				data-strict="1"
				data-reactions-enabled="1"
				data-emit-metadata="0"
				data-input-position="bottom"
				data-theme="' . $this->context['theme'] . '"
				data-lang="' . Lang::$txt['lang_dictionary'] . '"
				data-loading="lazy"
				crossorigin="anonymous"
				async
			>
			</script>';
	}
}
